package com.nzw.pojo.pay;

import com.nzw.common.Constant;
import com.nzw.utils.MD5Utils;

import javax.persistence.Column;
import javax.persistence.Table;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created by Administrator on 2018\3\3 0003.
 * 公众号支付信息实体类
 */

@Table(name = "t_payment")
public class Payment {

    private Long id;            //id
    private Long shopId;        //公司id
    private Long companyId;
    private String name;        //企业名称
    private String appid;       //公众号appid
    private String appSecret;   //公众号秘钥
    private String payMchid;    //支付号id
    private String paySecret;   //支付秘钥
    private byte[] certData;    //支付ssl证书


    @Column(name="id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    @Column(name = "shopId")
    public Long getShopId() {
        return shopId;
    }

    public void setShopId(Long shopId) {
        this.shopId = shopId;
    }

    @Column(name = "appid")
    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    @Column(name = "appSecret")
    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    @Column(name = "payMchid")
    public String getPayMchid() {
        return payMchid;
    }

    public void setPayMchid(String payMchid) {
        this.payMchid = payMchid;
    }

    @Column(name = "paySecret")
    public String getPaySecret() {
        return paySecret;
    }

    public void setPaySecret(String paySecret) {
        this.paySecret = paySecret;
    }


    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "companyId")
    public Long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    @Column(name = "certData")
    public byte[] getCertData() {
        return certData;
    }




    @Override
    public String toString() {
        return "Payment{" +
                "id=" + id +
                ", shopId=" + shopId +
                ", name='" + name + '\'' +
                ", appid='" + appid + '\'' +
                ", appSecret='" + appSecret + '\'' +
                ", payMchid='" + payMchid + '\'' +
                ", paySecret='" + paySecret + '\'' +
                ", certData=" + Arrays.toString(certData) +
                '}';
    }

    public void setCertData(byte[] certData) {
        this.certData = certData;
    }

    public String createPaySign(Map<String,String> params){

        List<String> list = new ArrayList<>(params.keySet());
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        String signTemp = "";
        for (String e : list) {
            signTemp += "&" + e + "=" + params.get(e);
        }
        signTemp = signTemp.substring(1);
        String stringSignTemp = signTemp + "&key=" + Constant.KEY;    //注：key为商户平台设置的密钥key
        System.out.println("签名参数设置========:"+stringSignTemp);
        String sign = MD5Utils.getMD5Code(stringSignTemp).toUpperCase();    //注：MD5签名方式
        //System.out.println("支付签名:"+sign);
        return sign;

    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }
}
